<template>
  <div class="app-container">
    <!-- 查询表单 - 布局和比例优化 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item prop="repairId">
            <template #label>
              <el-tooltip content="请输入故障处理记录的ID" placement="top">
                <span>故障处理</span>
              </el-tooltip>
            </template>
            <el-input
                v-model="queryParams.repairId"
                placeholder="请输入故障处理ID"
                clearable
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用户ID" prop="userId"> <!-- 查询条件标签保持为“用户ID” -->
            <el-input
                v-model="queryParams.userId"
                placeholder="请输入用户ID"
                clearable
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item prop="assignType">
            <template #label>
              <el-tooltip content="1-负责人,2-实施人" placement="top">
                <span>分配类型</span>
              </el-tooltip>
            </template>
            <el-select v-model="queryParams.assignType" placeholder="请选择分配类型" clearable style="width: 100%;">
              <el-option
                  v-for="dict in mission_personnel"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" style="text-align: right;">
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['fault:assign:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['fault:assign:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['fault:assign:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['fault:assign:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 主表格 - 列显示“用户名” -->
    <el-table v-loading="loading" :data="assignList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分配ID" align="center" prop="id" width="80" />
      <el-table-column label="故障处理ID" align="center" prop="repairId" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column label="用户名" align="center" prop="userName" min-width="120" :show-overflow-tooltip="true" /> <!-- [重要] 修改为显示用户名 -->
      <el-table-column align="center" prop="assignType" width="120">
        <template #header>
          <el-tooltip content="1-负责人,2-实施人" placement="top">
            <span>分配类型</span>
          </el-tooltip>
        </template>
        <template #default="scope">
          <dict-tag :options="mission_personnel" :value="scope.row.assignType"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['fault:assign:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['fault:assign:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改维修人员分配对话框 -->
    <el-dialog :title="title" v-model="open" width="550px" append-to-body>
      <el-form ref="assignRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item prop="repairId">
              <template #label>
                <el-tooltip content="输入故障处理记录的ID" placement="top">
                  <span>故障处理ID</span>
                </el-tooltip>
              </template>
              <el-input v-model="form.repairId" placeholder="请输入故障处理ID" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="用户ID" prop="userId"> <!-- 弹窗中保持为“用户ID”，用于数据输入和绑定 -->
              <el-input v-model="form.userId" placeholder="请输入用户ID" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item prop="assignType">
              <template #label>
                <el-tooltip content="1-负责人,2-实施人" placement="top">
                  <span>分配类型</span>
                </el-tooltip>
              </template>
              <el-select v-model="form.assignType" placeholder="请选择分配类型" style="width: 100%;">
                <el-option
                    v-for="dict in mission_personnel"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Assign">
import { listAssign, getAssign, delAssign, addAssign, updateAssign } from "@/api/fault/assign"

const { proxy } = getCurrentInstance()
const { mission_personnel } = proxy.useDict('mission_personnel')

const assignList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    repairId: null,
    userId: null, // 查询参数仍然是userId
    assignType: null,
  },
  rules: {
    repairId: [
      { required: true, message: "故障处理ID不能为空", trigger: "blur" }
    ],
    userId: [ // 校验规则依然是针对userId
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    assignType: [
      { required: true, message: "分配类型不能为空", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询维修人员分配列表 */
function getList() {
  loading.value = true
  listAssign(queryParams.value).then(response => {
    assignList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    repairId: null,
    userId: null, // 重置时 userId 保持为 null
    assignType: null,
  }
  proxy.resetForm("assignRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加维修人员分配"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getAssign(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改维修人员分配"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["assignRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateAssign(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addAssign(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除维修人员分配编号为"' + _ids + '"的数据项？').then(function() {
    return delAssign(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('fault/assign/export', {
    ...queryParams.value
  }, `assign_${new Date().getTime()}.xlsx`)
}

getList()
</script>

<style scoped>
/* 可以在这里添加一些样式来微调布局，例如： */
.el-row {
  margin-bottom: 10px; /* 增加行之间的间距，保持视觉平衡 */
}
.el-form-item {
  margin-bottom: 0; /* 在el-row中已经有间距，减少form-item自带的冗余间距 */
}
</style>
